<template>
  <div>
    <!-- 搜索 -->
    <el-form :inline=true class="query-box">
      <el-form-item label="分类名称">
        <el-input v-model="queryForm.categoryName" placeholder="请输入分类名称" class="handle-input"></el-input>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
    </el-form>
    <div class="option-box">
      <el-button type="primary" v-has="'category:add'" @click="addCategory">添加</el-button>
    </div>

    <el-table :data="tableData" stripe style="width: 100%" ref="multipleTable"
      v-loading="tableLoading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading">
      <el-table-column type="selection" />
      <el-table-column prop="categoryName" label="分类名称" />
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" v-has="'category:update'" @click="editCategory(scope.$index, scope.row)">编辑</el-button>
          <el-button type="text" class="text-button-del" v-has="'category:delete'" @click="delCategory(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination @current-change="pageChange" layout="prev, pager, next"
        :current-page="queryForm.pageNum" :page-size="queryForm.pageSize" :total="totalNum">
      </el-pagination>
    </div>

    <!--dialog弹窗-->
    <el-dialog :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="675px"
      :modal-append-to-body=false
      :close-on-click-modal=false
      :close-on-press-escape=false>
      <el-form ref="categoryForm" :model="categoryForm" :inline=true :rules="categoryFormRules" label-position="right" label-width="80px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="categoryForm.categoryName" placeholder="请输入分类名称" class="handle-input"></el-input>
        </el-form-item>
        <el-form-item label="备注">
            <el-input type="textarea" v-model="categoryForm.remark" placeholder="请输入备注" class="handle-textarea"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="loading"  @click="categoryFormSubmit">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as category from '@/api/category'
import {mapGetters} from 'vuex'

export default {
  name: 'CategoryList',
  data () {
    return {
      queryForm: {
        categoryName: '',
        pageNum: this.GLOBAL.pageNum,
        pageSize: this.GLOBAL.pageSize
      },
      categoryForm: {
        id: '',
        categoryName: '',
        remark: ''
      },
      dialogVisible: false, // 弹窗
      dialogTitle: '', // 弹窗标题
      isEdit: false, // 是否为修改操作
      totalNum: 1, // 总页数
      tableData: [], // 表格数据
      tableLoading: true,
      loading: false,

      categoryFormRules: {
        categoryName: [{required: true, message: '分类名称不能为空', trigger: 'blur'}],
      }
    }
  },
  methods: {
    pageChange (val) {
      this.queryForm.pageNum = val;
      this.queryList();
    },
    transformDict (key, code) {
      return this.dictinfo(key, code);
    },
    search () {
      // this.pageNum = this.GLOBAL.pageNum;
      this.queryList();
    },
    queryList () {
      let param = this.GLOBAL.handlerQueryParam({
        ...this.queryForm
      });
      category.categoryList(param).then(res => {
        this.tableData = res.data.list;
        this.totalNum = Number(res.data.total);
        this.tableLoading = false;
      });
    },
    addCategory () {
      this.isEdit = false;
      this.dialogTitle = '添加';
      this.GLOBAL.clearObject(this.categoryForm);
      this.dialogVisible = true;
    },
    editCategory (index, row) {
      this.isEdit = true;
      this.dialogTitle = '编辑';
      this.GLOBAL.clearObject(this.categoryForm);
      category.getById(row.id).then(res => {
        this.categoryForm = res.data;
        this.dialogVisible = true;
      });
    },
    delCategory (index, row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          category.delCategory(row.id)
            .then(res => {
              this.$message.success('删除成功');
              this.queryList();
            });
        })
    },
    categoryFormSubmit () {
      this.$refs.categoryForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          let param = this.GLOBAL.handlerQueryParam({
            ...this.categoryForm
          });
          if (this.isEdit) {
            category.editCategory(param)
              .then(res => {
                this.loading = false;
                this.dialogVisible = false;
                this.queryList();
              })
              .catch(() => {
                this.loading = false;
              })
          } else {
            category.addCategory(param)
              .then(res => {
                this.loading = false;
                this.dialogVisible = false;
                this.queryList();
              })
              .catch(() => {
                this.loading = false;
              });
          }
        }
      });
    }
  },
  created () {
    this.queryList();
  },
  computed: {
    ...mapGetters(['dictinfo', 'dictList']),
  },
  watch: {
    dialogVisible (val, oldValue) {
      if (val && this.$refs.categoryForm !== undefined) {
        this.$refs.categoryForm.resetFields();
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
